<template>
  <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    yName: {
      type: String,
      required: true
    },
    data: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      chart: null,
      listLoading: false,
      xDate: [],
      data_obj: {},
      data1: [],
      data2: []
    }
  },
  watch: {
    data: {
      handler(newValue) {
        let base = +new Date(2010, 9, 3)
        const oneDay = 24 * 3600 * 1000

        const date = []
        const data = [Math.round(Math.random() * -100)]

        for (let i = 1; i < 250; i++) {
          const now = new Date(base += oneDay)
          date.push([now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'))
          const _num = Math.round((Math.random() - 0.5) * 20 + data[i - 1])
          let num = 0
          if(_num < -100) {
            num = -100
          }else if(_num > 0) {
            num = 0
          }else {
            num = _num
          }
          data.push(num)
        }
        this.$nextTick(() => {
          this.initChart(date, data)
        })
      },
      deep: true,
      immediate: true
    }
  },
  created() {

  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(date, data) {
      this.chart = echarts.init(document.getElementById(this.id))

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          position: function(pt) {
            return [pt[0], '40%']
          },
          axisPointer: {
            type: 'shadow',
            shadowStyle: {
              color: 'rgba(150,150,150,0.3)'
            }
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: date
        },
        yAxis: {
          type: 'value',
          boundaryGap: [0, '100%'],
          min: -100,
          max: 0,
        },
        // dataZoom: [{
        //   type: 'inside',
        //   start: 0,
        //   end: 100
        // }, {
        //   start: 0,
        //   end: 100
        // }],
        series: [
          {
            name: '模拟数据',
            type: 'line',
            symbol: 'none',
            sampling: 'lttb',
            itemStyle: {
              color: 'rgb(255, 70, 131)'
            },
            lineStyle: {
              width: 2
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: 'rgb(255, 158, 68)'
              }, {
                offset: 1,
                color: 'rgb(255, 70, 131)'
              }])
            },
            data: data
          }
        ]
      })
    },
  }
}
</script>
